let logger = require("pomelo-logger").getLogger(__filename);
let BaseTableService = require("../base/tableService");
let util = require('util');
let YuanBaoTableCreator = require("./niuniuYuanBaoTable").create;
let PlayerCreator = require("./niuniuPlayer").create;
let uuid = require('node-uuid');

/*开放的快速匹配场个数*/
const QUICK_JOIN_ROOM_NUM = 3;

let Service = function (app) {
    BaseTableService.call(this, app);
    this.gid = 10001;
};

util.inherits(Service, BaseTableService);

Service.prototype.getGid = function () {
    return this.gid;
};

Service.prototype.doCreateSystemTable = function () {
    let self = this;
    let rooms = this.app.get("rooms");
    for (let i = 0; i < QUICK_JOIN_ROOM_NUM && i < rooms.length; i++) {
        let room = rooms[i];
        for (let i = 0; i < 50; i++) {
            self.doCreateYuanBaoTable({
                tid: uuid.v4(),
                code: "0",
                room: room,
                maxPlayers: 6,
                dealerType: 2,
                multiplierType: 1,
                maxAuctionDealerMultiplier: 3,
                visible: 1,
                creatorUid: 0,
                gid: 10001
            });
        }
        logger.info(`niuniu room rid=${room.rid} createSystemTable 50个`);
    }
};

Service.prototype.commit = function (frontendId, sessionId) {
    let table = this.findTableByPlayer(frontendId, sessionId);
    if (table == null) {
        return { resultCode: 404, message: "桌子不存在" };
    }
    let player = this.findPlayer(frontendId, sessionId);
    if (player == null) {
        return {resultCode: 404, message: "玩家不在线"};
    }
    if (!table.getIsPlaying()) {
        return { resultCode: 404, message: "游戏未开始"};
    }
    if (table.getStep() !== 3) {
        return { resultCode: 404, message: "桌子状态不对"};
    }
    if (player.getAction() !== 0) {
        return { resultCode: 404, message: "玩家已经操作了"};
    }
    table.commit(player);
    return { resultCode: 0, message: "ok"};
};

Service.prototype.bet = function (frontendId, sessionId, multiplier) {
    if (!multiplier) {
        return {resultCode: 404, message: "倍数为空"};
    }
    if (multiplier !== 1 && multiplier !== 2) {
        return { resultCode: 404, message: "倍数不对" };
    }
    let table = this.findTableByPlayer(frontendId, sessionId);
    if (table == null) {
        return { resultCode: 404, message: "桌子不存在"};
    }
    let player = this.findPlayer(frontendId, sessionId);
    if (player == null) {
        return { resultCode: 404, message: "玩家不在线"};
    }
    if (!table.getIsPlaying()) {
        return { resultCode: 404, message: "游戏未开始"};
    }
    if (table.getStep() !== 2) {
        return { resultCode: 404, message: "桌子状态不对"};
    }
    if (player.getAction() !== 0) {
        return { resultCode: 404, message: "玩家已经操作了"};
    }
    if (table.getDealerType() === 2 && table.getDealerUid() === player.getUid()) {
        return { resultCode: 404, message: "抢到庄的玩家不能再下注"};
    }
    table.bet(player, multiplier);
    return { resultCode: 0, message: "ok"};
};

Service.prototype.betForDealer = function (frontendId, sessionId, multiplier) {
    //0, 1, 2表示 不抢， 抢1倍，抢2倍
    if (multiplier !== 0 && multiplier !== 1 && multiplier !== 2) {
        return { resultCode: 404, message: "倍数不对" };
    }
    let table = this.findTableByPlayer(frontendId, sessionId);
    if (table == null) {
        return { resultCode: 404, message: "桌子不存在" };
    }
    let player = this.findPlayer(frontendId, sessionId);
    if (player == null) {
        return { resultCode: 404, message: "玩家不在线" };
    }
    if (!table.getIsPlaying()) {
        return { resultCode: 404, message: "游戏未开始"};
    }
    if (table.getStep() !== 1) {
        return { resultCode: 404, message: "桌子状态不对" };
    }
    if (player.getAction() !== 0) {
        return { resultCode: 404, message: "玩家已经操作了" };
    }
    table.betForDealer(player, multiplier);
    return {
        resultCode: 0,
        message: "ok"
    };
};

Service.prototype.doCreatePlayer = function (playerData, frontendId, sessionId) {
    return PlayerCreator(playerData, frontendId, sessionId);
};

Service.prototype.doValidateCreateTableParams = function (params) {
    if (params.maxPlayers !== 6 && params.maxPlayers !== 10) {
        logger.error('doValidateCreateTableParams failed invalid maxPlayers = ' + params.maxPlayers);
        return {resultCode: 404, message: "invalid maxPlayers"};
    }
    if (params.dealerType < 1 || params.dealerType > 5) {
        logger.error('invalid dealerType = ' + params.dealerType);
        return {resultCode: 404, message: "invalid dealerType"};
    }
    if (params.multiplierType !== 1 && params.multiplierType !== 2) {
        logger.error('invalid multiplierType = ' + params.multiplierType);
        return {resultCode: 404, message: "invalid multiplierType"};
    }
    if (params.maxAuctionDealerMultiplier < 1 || params.maxAuctionDealerMultiplier > 3) {
        logger.error('invalid maxAuctionDealerMultiplier = ' + params.maxAuctionDealerMultiplier);
        return {resultCode: 404, message: "invalid maxAuctionDealerMultiplier"};
    }
    if (params.canPlayerWithUnknown !== 1 && params.canPlayerWithUnknown !== 2) {
        logger.error('invalid canPlayerWithUnknown = ' + params.canPlayerWithUnknown);
        return {resultCode: 404, message: "invalid canPlayerWithUnknown"};
    }
    return {resultCode: 0, message: "ok"};
};

Service.prototype.doCreateYuanBaoTable  = function (tableCfg) {
    let table = YuanBaoTableCreator(tableCfg);
    this.tables.push(table);
    return table;
};

Service.prototype.doPlayerReconnect = function(table, player) {
    player.push("onInitializeNiuniuTable", {
        table: table.toJSON(),
        myCards : player.getDisplayCards(table)
    });
};

module.exports.create = function (app) {
    return new Service(app);
};